package top.stmo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 *      -------------------------Request获取请求数据--------------------------
 *
 *      如果是GET请求，请求参数在请求行中
 *      如果是POST请求，请求参数一般在请求体中
 *
 *     对于请求参数的获取,常用的有以下两种:
 *
 *      GET方式:       String getQueryString()
 *      POST方式:      BufferedReader getReader()
 *
 *      此处大家需要注意的是GET请求和POST请求接收参数的方式不一样
 *
 *
 *      -------------------------获取请求参数的通用方式--------------------------
 *
 *      Map<String,String[]> getParameterMap()          获取所有参数Map集合
 *      String[] getParameterValues(String name)        根据名称获取参数值（数组）
 *      String getParameter(String name)                根据名称获取参数值(单个值)
 *
 */
@WebServlet("/req2")
public class Request2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                                            throws ServletException, IOException {
        System.out.println("get");
        Map<String, String[]> map = req.getParameterMap();
        for (String s : map.keySet()) {
            System.out.print(s + "：");
            String[] strings = map.get(s);
            for (String s1 : strings) {
                System.out.print(s1 + " ");
            }
            System.out.println();
        }

        System.out.println("---------------------");

        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        System.out.println("---------------------");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username);
        System.out.println(password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                                        throws ServletException, IOException {
        System.out.println("post");
        Map<String, String[]> map = req.getParameterMap();
        for (String s : map.keySet()) {
            System.out.print(s + "：");
            String[] strings = map.get(s);
            for (String s1 : strings) {
                System.out.print(s1 + " ");
            }
            System.out.println();
        }

        System.out.println("---------------------");

        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        System.out.println("---------------------");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username);
        System.out.println(password);
    }
}
